//
//  YDContactRecentMeetsCell.m
//  IngCard
//
//  Created by kf on 2017/10/18.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDContactRecentMeetsCell.h"


@implementation YDContactRecentMeetsCellModel
-(Class) viewClass
{
    return [YDContactRecentMeetsCell class];
}

@end


@interface YDContactRecentMeetsCell()
@property(nonatomic, strong) UIImageView *calendarImageView;
@property(nonatomic, strong) UILabel *dateLabel;
@property(nonatomic, strong) UILabel *timingLabel;

@property(nonatomic, strong) UIImageView *editImageView;
@property(nonatomic, strong) UIButton *editButton;

@property(nonatomic, strong) UIImageView *editingImageView;
@property(nonatomic, strong) UILabel *contentLabel;
@property(nonatomic, strong) UIImageView *contentBackground;

@property(nonatomic, strong) UIView *lineView;

@end

@implementation YDContactRecentMeetsCell


+(CGFloat)viewHeight:(YDContactRecentMeetsCellModel *) dataSource
{
    return dataSource.height ?:  70;
}

- (void) initSubviews
{
    [self.contentView addSubview:self.calendarImageView];
    [self.calendarImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(40);
        make.top.equalTo(16);
        make.width.equalTo(20);
        make.height.equalTo(20);
    }];
    
    [self.contentView addSubview:self.dateLabel];
    [self.dateLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.calendarImageView.mas_right).offset(15);
        make.top.equalTo(16);
        make.height.equalTo(20);
    }];
    
    [self.contentView addSubview:self.timingLabel];
    [self.timingLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dateLabel);
        make.top.equalTo(self.dateLabel.mas_bottom).offset(2);
        make.width.lessThanOrEqualTo(300);
        make.height.equalTo(20);
    }];
    
    [self.contentView addSubview:self.editImageView];
    [self.editImageView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-70);
        make.top.equalTo(44);
        make.width.equalTo(15);
        make.height.equalTo(15);
    }];
    
    [self.contentView addSubview:self.editButton];
    [self.editButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.editImageView.mas_right).offset(1);
        make.centerY.equalTo(self.editImageView);
        make.width.equalTo(48);
        make.height.equalTo(17);
    }];
    
    [self.contentView addSubview:self.lineView];
    [self.lineView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.width.equalTo(SCREEN_WIDTH);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-1);
        make.height.equalTo(1);
    }];
}

-(void) onViewModelChanged
{
    YDContactRecentMeetsCellModel *dataSource = (YDContactRecentMeetsCellModel *)self.dataSource;
    if (dataSource)
    {
        self.dateLabel.text = dataSource.date;
        self.timingLabel.text = dataSource.timing;
        
        [self.dateLabel sizeToFit];
        [self.timingLabel sizeToFit];
        
        
        if (dataSource.content && (dataSource.content.length > 0))
        {
            self.editButton.hidden = YES;
            self.editImageView.hidden = YES;
            
            NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
            // 对齐方式
            style.alignment = NSTextAlignmentJustified;
            // 首行缩进
            style.firstLineHeadIndent = 15.0f;
            // 头部缩进
            style.headIndent = 15.0f;
            // 尾部缩进
            style.tailIndent = -10.0f;
            NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:dataSource.content attributes:@{ NSParagraphStyleAttributeName : style}];
            self.contentLabel.attributedText = attrText;
            
        
            CGSize size = CGSizeMake(SCREEN_WIDTH - 70, MAXFLOAT);
            
            NSMutableParagraphStyle *editstyle = [[NSMutableParagraphStyle alloc] init];
            NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading;
            [editstyle setLineBreakMode:NSLineBreakByWordWrapping];
            CGRect frame = [dataSource.content boundingRectWithSize:size
                                                            options:options
                                                         attributes:@{NSFontAttributeName: UIRegularFontMake(13), NSParagraphStyleAttributeName : editstyle} context:nil];
            Log(@"%f",dataSource.height);
            
            dataSource.height = frame.size.height + 70 + 35;
            
            [self removeEditView];
            [self addEditView:frame.size.height + 35];
        }
        else
        {
            self.editButton.hidden = NO;
            self.editImageView.hidden = NO;
            
            self.dataSource.height = 70;
            [self removeEditView];
        }
    }
}

-(void) addEditView:(CGFloat) height
{
    [self.contentView addSubview:self.editingImageView];
    [self.editingImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(23);
        make.top.equalTo(52);
        make.width.equalTo(18);
        make.height.equalTo(18);
    }];
    
    [self.contentView addSubview:self.contentBackground];
    [self.contentBackground makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(25);
        make.right.equalTo(-20);
        make.top.equalTo(55);
        make.height.equalTo(height);
    }];

    [self.contentView addSubview:self.contentLabel];
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(33);
        make.right.equalTo(self.contentView.mas_right).offset(-36);
        make.top.equalTo(55);
        make.height.equalTo(height);
    }];
}

-(void) removeEditView
{
    if ([self.contentView.subviews containsObject:self.editingImageView])
    {
        [self.editingImageView removeConstraints:self.editingImageView.constraints];
        [self.editingImageView removeFromSuperview];
    }
    
    if ([self.contentView.subviews containsObject:self.contentBackground])
    {
        [self.contentBackground removeConstraints:self.contentBackground.constraints];
        [self.contentBackground removeFromSuperview];
    }
    
    if ([self.contentView.subviews containsObject:self.contentLabel])
    {
        [self.contentLabel removeConstraints:self.contentLabel.constraints];
        [self.contentLabel removeFromSuperview];
    }
}
#pragma mark - private
- (void)edit
{
    YDContactRecentMeetsCellModel *dataSource = (YDContactRecentMeetsCellModel *)self.dataSource;
    if (dataSource)
    {
        BlockGoGo(dataSource.edit);
    }
}

#pragma mark - lazy load

-(UIImageView *) calendarImageView
{
    if(!_calendarImageView)
    {
        _calendarImageView = [UIImageView new];
        _calendarImageView.image = UIImageMake(@"Contact_detail_Calendar");
        _calendarImageView.contentMode = NSTextAlignmentLeft;
        [_calendarImageView sizeToFit];
    }
    return _calendarImageView;
}

-(UIImageView *) editImageView
{
    if (!_editImageView)
    {
        _editImageView = [UIImageView new];
        _editImageView.image = UIImageMake(@"myCard_activity_edit");
        _editImageView.contentMode = NSTextAlignmentLeft;
        [_editImageView sizeToFit];
    }
    return _editImageView;
}

//contact_content_background

-(UIImageView *) editingImageView
{
    if (!_editingImageView)
    {
        _editingImageView = [UIImageView new];
        _editingImageView.image = UIImageMake(@"contact_detail_editing");
        _editingImageView.contentMode = NSTextAlignmentLeft;
        [_editingImageView sizeToFit];
    }
    return _editingImageView;
}

-(UIButton *) editButton
{
    if (!_editButton)
    {
        _editButton = [[UIButton alloc] init];
        _editButton.frame = CGRectMake(0, 0, 48, 17);
        
        [_editButton setTitle:@"添加描述" forState:(UIControlStateNormal)];
        [_editButton setTitleColor:UIColorMake(151, 151, 151) forState:UIControlStateNormal];
        _editButton.titleLabel.font = UIRegularFontMake(12);
        
        [_editButton addTarget:self
                        action:@selector(edit)
              forControlEvents:UIControlEventTouchUpInside];
    }
    return _editButton;
}


-(UILabel *) dateLabel
{
    if (!_dateLabel)
    {
        _dateLabel = [UILabel new];
        _dateLabel.text = @"Experience";
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.font = UIMediumFontMake(16);
        _dateLabel.textColor = UIColorMake(21, 26, 65);
    }
    return _dateLabel;
}

-(UILabel *) timingLabel
{
    if (!_timingLabel)
    {
        _timingLabel = [UILabel new];
        _timingLabel.text = @"Experience";
        _timingLabel.textAlignment = NSTextAlignmentLeft;
        _timingLabel.font = UIMediumFontMake(16);
        _timingLabel.textColor = UIColorMake(21, 26, 65);
    }
    return _timingLabel;
}

-(UIImageView *) contentBackground
{
    if (!_contentBackground)
    {
        _contentBackground = [UIImageView new];
        UIImage *image = UIImageMake(@"contact_content_background");
        image = [image  stretchableImageWithLeftCapWidth:image.size.width*0.3 topCapHeight:image.size.height*0.7];
        _contentBackground.image = image;
        _contentBackground.contentMode = UIViewContentModeScaleToFill;
    }
    return _contentBackground;
}

-(UILabel *) contentLabel
{
    if (!_contentLabel)
    {
        _contentLabel = [UILabel new];
        _contentLabel.text = @"Experience";
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = UIRegularFontMake(13);
        _contentLabel.textColor = UIColorMake(21, 26, 65);
        _contentLabel.userInteractionEnabled=YES;
        
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(edit)];
        [_contentLabel addGestureRecognizer:labelTapGestureRecognizer];
    }
    return _contentLabel;
}

- (UIView *) lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 123, 1)];
        _lineView.backgroundColor = UIColorMake(221, 221, 221);
    }
    return _lineView;
}
@end

